_realname=libbacktrace
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_commit='1da441c1b020bef75d040dd300814b1a49e0e529'
pkgver=r53.1da441c
pkgrel=2
pkgdesc="Library to produce symbolic backtraces (mingw-w64)"
arch=('any')
url="https://github.com/ianlancetaylor/libbacktrace"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=('git')
source=("git+https://github.com/ianlancetaylor/libbacktrace.git#commit=${_commit}"
        "libbacktrace.pc")
sha256sums=('SKIP'
            '52e75395d85a1c15a6045c8ae7f91e29b8756f430a428c6def255ca5885ee661')

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "r%s.%s" "$(git rev-list --count "${_commit}")" "$(git rev-parse --short "${_commit}")"
}

prepare () {
  cd "${srcdir}/${_realname}"

  # dont allow undefined symbols to build the dll
  sed -i "87ilibbacktrace_la_LDFLAGS = -no-undefined" Makefile.am
  autoreconf -vfi
}

build() {
  cd "${srcdir}/${_realname}"

  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  ../${_realname}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-static \
    --enable-shared

  make
}

package() {
  cd "${srcdir}"/build-${CARCH}

  make install DESTDIR="${pkgdir}"

  install -Dm644 ${srcdir}/${_realname}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  
  # pkg-config
  install -Dm644 "${srcdir}/${_realname}.pc" "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/${_realname}.pc"
}
